package com.example.sls.controller.teacher.publish;

import com.example.sls.pojo.teacher.publish.Publish;
import com.example.sls.service.teacher.publish.PublishService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/publish")
public class PublishController {

    @Autowired
    private PublishService publishService;

    //在指定班级发布题集
    @PostMapping("/publishSet")
    public Map<String, String>publishSet(@RequestParam Map<String, String> params){
        Map<String, String> res = new HashMap<>();

        Integer setId = Integer.valueOf(params.get("setId"));
        Integer classId = Integer.valueOf(params.get("classId"));

//        System.out.println("========");
//        System.out.println(setId);
//        System.out.println(classId);
//        System.out.println("========");


        Integer id = null;

        if( publishService.getMaxId() == null){
            id = 1;
        }
        else{
            id = publishService.getMaxId() + 1;
        }
        Date publishDate = new Date();

        Publish publish = new Publish(id, setId, classId, publishDate);

        if(publishService.publishSet(publish)){
            res.put("error_message", "success");
        }
        else{
            res.put("error_message", "发布出错");
        }

        return res;
    }

    @PostMapping("/deletePublishInfo")
    public Map<String, String> deletePublishInfo(@RequestParam Map<String, String> params){

        Map<String, String> res = new HashMap<>();

        Integer setId = Integer.valueOf(params.get("setId"));
        Integer classId = Integer.valueOf(params.get("classId"));

//        System.out.println("========");
//        System.out.println(setId);
//        System.out.println(classId);
//        System.out.println("========");

        if(publishService.deleteSet(setId, classId)){
            res.put("error_message", "success");
        }
        else{
            res.put("error_message", "删除出错");
        }

        return res;
    }

    @GetMapping("/getPageInfoByCId/{CId}/{pageNum}/{pageSize}")
    public PageInfo<Publish> getPublishInfoByCId(@PathVariable("CId") Integer CId,@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        return publishService.getPublishInfoByCId(CId, pageNum, pageSize);
    }

    //获取在指定班级未发布过的题集的setId
    @GetMapping("/getUnpublished")
    public List<Integer> getUnpublishedSetId(@RequestParam Map<String, String> params){

        Integer teacherId = Integer.valueOf(params.get("teacherId"));
        Integer classId = Integer.valueOf(params.get("classId"));

//        System.out.println("========");
//        System.out.println(teacherId);
//        System.out.println(classId);
//        System.out.println("========");

        return publishService.getUnpublishedSetId(teacherId, classId);
    }




}
